package net.sourceforge.peertwitter.mbpast;

import java.util.ArrayList;
import java.util.List;

import net.sourceforge.peertwitter.core.Profile;
import net.sourceforge.peertwitter.core.messaging.TweetMessage;
import net.sourceforge.peertwitter.scamp.Swarm;
import rice.environment.Environment;
import rice.p2p.commonapi.Id;

public class ReplicationImpl implements Replication {
	private static final long serialVersionUID = -6129076831763003416L;
	private List<TweetMessage>tweets;
	private Swarm swarm;
	private Profile profile;
	@SuppressWarnings("unused")
	private final Environment env;
	
	
	public ReplicationImpl(Profile profile, Environment env) {
		this.profile = profile;
		this.env = env;
		this.swarm = new Swarm(profile.getId(), env);
		this.tweets = new ArrayList<TweetMessage>();
	}
	
	/* (non-Javadoc)
	 * @see br.ufsc.das.mbpast.ReplicationI#isComplete()
	 */
	@Override
	public boolean isComplete() {
		return true;
	}
	
	/* (non-Javadoc)
	 * @see br.ufsc.das.mbpast.ReplicationI#getId()
	 */
	@Override
	public Id getId() {
		return this.profile.getId();
	}
	
	public void setProfile(Profile profile) {
		this.profile = profile;
	}
	
	/* (non-Javadoc)
	 * @see br.ufsc.das.mbpast.ReplicationI#getProfile()
	 */
	@Override
	public Profile getProfile() {
		return this.profile;
	}
	
	/* (non-Javadoc)
	 * @see br.ufsc.das.mbpast.ReplicationI#setSwarm(br.ufsc.das.scamp.Swarm)
	 */
	@Override
	public void setSwarm(Swarm swarm) {
		this.swarm = swarm;
	}
	
	/* (non-Javadoc)
	 * @see br.ufsc.das.mbpast.ReplicationI#getSwarm()
	 */
	@Override
	public Swarm getSwarm() {
		return this.swarm;
	}
	
	/* (non-Javadoc)
	 * @see br.ufsc.das.mbpast.ReplicationI#addTweets(java.util.List)
	 */
	@Override
	public void addTweets(List<TweetMessage> messages) {
		getLastUpdates().addAll(messages);
	}
	
	/* (non-Javadoc)
	 * @see br.ufsc.das.mbpast.ReplicationI#addTweet(br.ufsc.das.peerTwitter.messaging.TweetMessage)
	 */
	@Override
	public void addTweet(TweetMessage tweetMessage) {
		if (!getLastUpdates().contains(tweetMessage)) {
			getLastUpdates().add(tweetMessage);
		}
		else {
			System.out.println("storing repeated tweet");
		}
	}

	
	/* (non-Javadoc)
	 * @see br.ufsc.das.mbpast.ReplicationI#setUpdates(java.util.List)
	 */
	@Override
	public void setUpdates(List<TweetMessage> updates) {
		this.tweets = updates;
	}
	
	/* (non-Javadoc)
	 * @see br.ufsc.das.mbpast.ReplicationI#getLastUpdates()
	 */
	@Override
	public List<TweetMessage> getLastUpdates() {
		if (tweets == null)
			tweets = new ArrayList<TweetMessage>();
		return tweets;
	}
	
	/* (non-Javadoc)
	 * @see br.ufsc.das.mbpast.ReplicationI#getTimestamp()
	 */
	@Override
	public ReplicationTimestamp getTimestamp() {
		return this;
	}

	@Override
	public int compareTo(ReplicationTimestamp o) {
		throw new RuntimeException("implement combine");
		//return 0;
	}

	/* (non-Javadoc)
	 * @see br.ufsc.das.mbpast.ReplicationI#getProfileTimestamp()
	 */
	@Override
	public long getProfileTimestamp() {
		return profile.getTimestamp();
	}

	/* (non-Javadoc)
	 * @see br.ufsc.das.mbpast.ReplicationI#getLastUpdateTimestamp()
	 */
	@Override
	public long getLastUpdateTimestamp() {
		if (tweets.isEmpty())
			return -1;
		return tweets.get(tweets.size()-1).getTimestamp();
	}

	/* (non-Javadoc)
	 * @see br.ufsc.das.mbpast.ReplicationI#getSwarmViewTimestamp()
	 */
	@Override
	public long getSwarmViewTimestamp() {
		return swarm.getDate();
	}

	/* (non-Javadoc)
	 * @see br.ufsc.das.mbpast.ReplicationI#combine(br.ufsc.das.mbpast.MBReplicaTimestamp)
	 */
	@Override
	public ReplicationTimestamp combine(ReplicationTimestamp timestamp) {
		throw new RuntimeException("implement combine");
	}
}
